package it.lupo.core.gui.controller;

import it.lupo.core.LupoSession;
import it.lupo.core.gui.MainApp;
import it.lupo.model.User;
import it.lupo.model.service.UserService;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

import org.apache.log4j.Logger;

public class LoginController {

	public static final String FXML_FILE = "src/main/resources/login.fxml";
	
	private static Logger logger = Logger.getLogger(LoginController.class);
	@FXML
	private TextField username;
	
	@FXML 
	private TextField password;
	
	@FXML
	private Label message;
	
	private UserService service = UserService.getInstance(); 
	
	@FXML
	private void handleLogin() {
	
		User user = service.check(username.getText(), password.getText());
		if(user!=null)		
		{
			message.setText("Success");
			try {
				logger.info("success login for usr = "+username.getText()+" psw = ********** ");
				LupoSession.getCurrent().login(user);
				MainApp.getInstance().change(BarCodeController.FXML_FILE);
	        } catch (Exception ex) {
	            logger.warn("failed login for usr = "+username.getText()+" psw = ********** ");
	            ex.printStackTrace();
	        }
		}
		else		
			message.setText("Wrong username and password");
	}
}
